var RpcFrame = require('./frame/RpcFrame.js');
var server = require('./ServerBook.js');
var child_process = require('child_process');

var rpcframe = new RpcFrame();

var isChild = process.env.CHILD == '1';
if (isChild) {
	var svr = rpcframe.createClient(server, function() {
		console.log('client done');

		// path, arg, cb
		rpcframe.rpc('path', 'arg', function(err, result) {
			console.log('rpc cb=%s', result);
		});
	});
	return;
}


var svr = rpcframe.createServer(server, function() {
	console.log('server done');

	console.log('-----------now create child process');

	var child = child_process.exec('CHILD=1 node ' + __filename, function(error, stdout, stderr) {
		if (error) {
			console.log(error.stack);
			console.log('Error code: ' + error.code);
			console.log('Signal received: ' + error.signal);
		}
		console.log('Child Process STDOUT: ' + stdout);
		console.log('Child Process STDERR: ' + stderr);
	});

	child.on('exit', function(code) {
		console.log('Child process exited with exit code ' + code);
	});

	// env not work
	// child_process.fork(__filename, null, {
	// 	env: {
	// 		'CHILD': '1'
	// 	}
	// });

	// // not work
	// var spawn = require('child_process').spawn;
	// var start = spawn('node', [__filename], {
	// 	env: {
	// 		'CHILD': '1'
	// 	}
	// });
	// start.stdout.pipe(process.stdout);

});